﻿using Dapper;
using Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IDAL
{
    public interface IDapperRepository
    {
        #region GetConection
        /// <summary>
        /// 获取一个数据库连接对象
        /// </summary>
        /// <returns></returns>
        IDbConnection GetConection();
        #endregion


        /// <summary>
        /// 插入实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool InsertEntity<T>(string cmd, T entity) where T : class, new();

        /// <summary>
        /// 根据主键获取实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        T GetEntityById<T>(string cmd, int id) where T : class, new();

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool UpdateEntity<T>(string cmd, T entity) where T : class, new();

        /// <summary>
        /// 物理删除，根据主键
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        bool DeleteEntityById<T>(string cmd, int id) where T : class, new();

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        bool LogicDeleteEntity<T>(string cmd, DynamicParameters param) where T : class, new();

        /// <summary>
        /// 异步查询-获取一个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd">sql语句</param>
        /// <param name="param">参数</param>
        /// <param name="flag">true存储过程，false sql语句</param>
        /// <returns></returns>
        Task<T> FindOneAsync<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new();

        /// <summary>
        /// 异步查询数据集合
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="cmd">sql语句</param>
        /// <param name="param">参数</param>
        /// <param name="flag">true存储过程，false sql语句</param>
        /// <returns>t</returns>
        Task<IList<T>> FindToListAsync<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new();

        /// <summary>
        /// 同步查询数据集合
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="connection">连接字符串</param>
        /// <param name="cmd">sql语句</param>
        /// <param name="param">参数</param>
        /// <param name="flag">true存储过程，false sql语句</param>
        List<T> FindToList<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new();

        /// <summary>
        /// 增、删、改同步操作
        ///  </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="connection">链接字符串</param>
        /// <param name="cmd">sql语句</param>
        /// <param name="param">参数</param>
        /// <param name="flag">true存储过程，false sql语句</param>
        /// <returns>int</returns>
        int ExcuteNonQuery<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new();


        /// <summary>
        /// 同步获取一个实体
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="connection">链接字符串</param>
        /// <param name="cmd">sql语句</param>
        /// <param name="param">参数</param>
        /// <param name="flag">true存储过程，false sql语句</param>
        /// <returns></returns>
        T FindOne<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new();

        DataTable FindTable(PagerInfo info, ref int recordCount, ref int pageCount);

        /// <summary>
        /// 同步查询操作
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="connection">连接字符串</param>
        /// <param name="cmd">sql语句</param>
        /// <param name="param">参数</param>
        /// <param name="flag">true存储过程，false sql语句</param>
        /// <returns>object</returns>
        object ExecuteScalar<T>(string cmd, DynamicParameters param, bool flag = true) where T : class, new();

    }
}
